<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model :model="model" :form="form" :rules="rules" ref="form">
        <a-row class="form-row" :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="12">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="公司" prop="company">
<!--              <a-input placeholder="请输入公司名称" v-model="model.company" style="width: 100%" />-->
              <j-dict-select-tag style="width: 100%" placeholder="请选择公司" v-model="model.company" dictCode="crm_company,name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="设备号" prop="deviceNo">
              <a-input placeholder="请输入设备号" v-model="model.deviceNo" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="12">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="楼层" prop="floor">
              <j-dict-select-tag placeholder="请选择楼层" v-model="model.floor" dictCode="floor" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="工序" prop="workingProcedure">
              <j-dict-select-tag placeholder="请选择工序" v-model="model.workingProcedure" dictCode="working_procedure" />
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="12">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="车间" prop="workshop">
              <j-dict-select-tag placeholder="请选择车间" v-model="model.workshop" dictCode="workshop" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="设备编号" prop="productCode">
              <a-input placeholder="请输入设备编号" v-model="model.productCode" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

  <script>
import { httpAction } from '@/api/manage'
import JEditor from '@/components/jeecg/JEditor'
import JSwitch from '@/components/jeecg/JSwitch'
export default {
  name: 'DeviceModal',

  components: {
    JEditor,
    JSwitch,
  },
  data() {
    return {
      title: '操作',
      visible: false,
      disable: true,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      rules: {
        company: [{ required: true, message: '请输入公司', trigger: 'blur' }],
        deviceNo: [{ required: true, message: '请输入设备号', trigger: 'blur' }],
        floor: [{ required: true, message: '请输入楼层', trigger: 'blur' }],
        workingProcedure: [{ required: true, message: '请输入工序', trigger: 'blur' }],
        workshop: [{ required: true, message: '请输入车间', trigger: 'blur' }],
        productCode: [{ required: true, message: '请输入设备编号', trigger: 'blur' }],
      },
      url: {
        add: '/crm_device/crmDevice/add',
        edit: '/crm_device/crmDevice/edit',
      },
    }
  },
  created() {},
  methods: {
    add() {
      this.disable = false
      this.model = Object.assign({}, {})
      this.edit({}, 1)
    },
    edit(record, isEdit) {
      if (isEdit != 1) {
        this.model = Object.assign({}, record)
      }

      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.disable = true
      this.$refs.form.resetFields();
    },

    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model)
          console.log('formData', formData)
          //时间格式化
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
              } else {
                this.$message.warning(res.message)
              }
            })
            .finally(() => {
              this.confirmLoading = false
              this.close()
            })
        } else {
          return false
        }
      })
    },

    handleCancel() {
      this.close()
    },
  },
}
</script>

  <style scoped></style>
